package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhaoxw
 * @description TODO
 * @date 2024/1/27 15:36
 */
@RestController
@Api(tags = "分类")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/save")
    @ApiOperation("新增种类")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        categoryService.add(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result page(@RequestBody CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult<CategoryDTO> page = categoryService.page(categoryPageQueryDTO);
        return Result.success(page);
    }

    @ApiOperation("根据id删除分类")
    @GetMapping("/del/{id}")
    public Result delById(@PathVariable Long id) {
        categoryService.delById(id);
        return Result.success();
    }

    @ApiOperation("更新分类")
    @PostMapping("/update")
    public Result<Category> update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }
    @ApiOperation("根据类型查询分类")
    @GetMapping("/queryByType")
    public Result queryByType(@Param("type")Integer type){
        List<Category> categories=categoryService.queryByType(type);
        return Result.success(categories);
    }


}
